package com.jcnet.bizaia;
import java.util.Random;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.jcnet.bizaia.bean.SPSetting;
import com.jcnet.bizaia.bean.SPUserBean;
import com.jcnet.bizaia.tools.DataStorageHelper;
import com.jcnet.bizaia.tools.JCTools;
import com.jcnet.bizaia.xstream.ACache;
public class SettingAcivity extends SherlockActivity implements OnClickListener{

	private DataStorageHelper dataStorageHelper;
	
	private DataStorageHelper settingDataStorageHelper;
	
	private TextView  settingAdressName;
	
	private ACache aCache;
	
	
	private TextView catcheTextView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		aCache = ACache.get(this);
		setContentView(R.layout.setting);
		
		catcheTextView = (TextView)findViewById(R.id.catcheTextView);
		
		settingAdressName = (TextView)findViewById(R.id.settingAdressName);
		dataStorageHelper = new DataStorageHelper(SettingAcivity.this, SPUserBean.dataTableNameForUser);
		
		settingDataStorageHelper = new DataStorageHelper(this, SPSetting.SETTING_TABLE_NAME);
		Random  random = new Random();
		
		catcheTextView.setText(random.nextInt(100)+"MB");
		
		initActionBars();
	}

	private void initActionBars() {
		// TODO Auto-generated method stub
		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayShowTitleEnabled(true);   //是都显示标题
		actionBar.setDisplayHomeAsUpEnabled(true);  //是都显示返回箭头
		getSupportActionBar().setDisplayShowHomeEnabled(false);   //   是都显示logo
		actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.top_bg));
		actionBar.show();
		setTitle(getString(R.string.setting));
	}


	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		
		
		
	}
	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:
			
			finish();
			
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(JCTools.isLogin(SettingAcivity.this)){
			((TextView)findViewById(R.id.userName)).setText(dataStorageHelper.get(SPUserBean.nikeNameSP_2, "").toString());
		}else{
			((TextView)findViewById(R.id.userName)).setText(getString(R.string.noLogin));
		}
		
		//重新设置区域
		settingAdressName.setText(settingDataStorageHelper.get(SPSetting.CHOOSE_COUNTRY_NAME)
				+
				" "
				+
				settingDataStorageHelper.get(SPSetting.CHOOSE_PREFECTURE_NAME)
				);
	
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch (v.getId()) {
		case R.id.suggestLin:
			
			startActivity(new Intent(SettingAcivity.this, SuggestActivity.class));
			break;
		case R.id.userAccount:
			
			
			if(JCTools.isLogin(SettingAcivity.this)){
				startActivity(new Intent(SettingAcivity.this, UserInfoActivity.class));
			}else{
				//没有登录呀
				startActivity(new Intent(SettingAcivity.this, LoginActivity.class));
			}
			
			break;
		case R.id.settingAdressLin:
			startActivity(new Intent(SettingAcivity.this, SettingAdressActivity.class));
			break;

			
		case R.id.settingHelpLin:   //帮助
			
			break;
			
		case R.id.settingAboutUsLin:  //关于我们
			
			startActivity(new Intent(SettingAcivity.this, AboutUsActivity.class));
			break;
			
		case R.id.cleanCatechLin:
			aCache.clear();
			catcheTextView.setText("0MB");
			break;
		default:
			break;
		}
		
	}
}
